package lainey.ui.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 缓存工具类
 */
public class SharedPreferencesUtil {

    // 缓存文件名
    public static final String PREFERENCE_FILE_NAME = "file_obj";

    /**
     * 读取一个对象
     */
    public static Object readObj(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        String reply = preferences.getString(key, "");
        Object obj = null;
        ObjectInputStream ois = null;
        ByteArrayInputStream bis = null;
        try {
            byte[] bytes = reply.getBytes();
            // 读取字节
            byte[] base64 = Base64.decodeBase64(bytes);
            // 封装到字节读取流
            bis = new ByteArrayInputStream(base64);
            // 封装到对象读取流
            ois = new ObjectInputStream(bis);
            // 读取对象
            obj = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    /**
     * 存储一个对象
     */
    public static <T> void saveObj(Context context, T obj, String key) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream bos = null;
        try {
            SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
            // 创建字节输出流
            bos = new ByteArrayOutputStream();
            // 创建对象输出流,封装字节流
            oos = new ObjectOutputStream(bos);
            // 将对象写入字节流
            oos.writeObject(obj);
            // 将字节流编码成base64的字符串
            byte[] byteArray = bos.toByteArray();
            byte[] bytes = Base64.encodeBase64(byteArray);
            String listBase64 = new String(bytes);
            Editor editor = preferences.edit();
            editor.putString(key, listBase64);
            editor.apply();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 清除某个key对应的缓存
     */
    public static void clearByKey(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString(key, "");
        editor.apply();
    }


}
